<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Arenacontest;
use App\Models\ChallengeGame;
use App\Models\LuckMoneyRule;
use App\Models\Mission;
use App\Models\Reservation;
use App\Models\Sect;
use App\Models\ShareLog;
use App\Models\WechatUser;
use App\Models\Withdraw;
use Carbon\Carbon;
use Encore\Admin\Layout\Content;
use Illuminate\Support\Facades\DB;

class HomeController extends Controller
{
    public function index(Content $content)
    {
        $today = Carbon::today();
        $todayStr = $today->format('Y-m-d');
        $today = $today->subDays(2);

        //总用户
        $totUserCount = WechatUser::count();
        //PK赛
        $pkCount = ChallengeGame::count();
        //乒乓币发放
        $missionsCount = Mission::count();
        //当天pk分享
        $pkShareCount = ShareLog::where([['created_at', '>=', $today], ['page', 'myChallengeGameInfo']])->count();
        //约战分享
        $reservationShareCount = ShareLog::where([['page', 'reservationInfo']])->count();
        //推广
        $shareCount=$pkShareCount+$reservationShareCount;

        //平台活跃度
        $activation = DB::select('select numbers.*,CASE WHEN a.item1 is null THEN 0 ELSE a.item1 END as item1,CASE WHEN a.item2 is null THEN 0 ELSE a.item2 END as item2 from (SELECT DATE_SUB(CURDATE(), INTERVAL n DAY) AS past_date FROM (
    SELECT 6 AS n UNION ALL SELECT 5 UNION ALL SELECT 4 UNION ALL SELECT 3 UNION ALL SELECT 2 UNION ALL SELECT 1 UNION ALL SELECT 0) numbers) numbers LEFT JOIN (select a.*,b.item2 from ( SELECT 
    DATE(created_at) AS date,COUNT(*) AS item1 FROM wechat_users WHERE created_at >= CURDATE() - INTERVAL 7 DAY and openid is not null and wechat_openid is null
    GROUP BY DATE(created_at) ORDER BY created_at ASC) a LEFT JOIN (SELECT DATE(created_at) AS date,COUNT(*) AS item2 FROM wechat_users WHERE 
    created_at >= CURDATE() - INTERVAL 10 DAY and openid is null and wechat_openid is not null GROUP BY DATE(created_at) ORDER BY created_at ASC) b on a.date=b.date) a on numbers.past_date=a.date');
        $item1Values = array_column($activation, 'item1');
        $item2Values = array_column($activation, 'item2');
        $item1=json_encode($item1Values);
        $item2=json_encode($item2Values);

        //球馆信息
        $sectList=Sect::select()
            ->limit(6)
            ->orderBy('created_at', 'desc')
            ->get();
        //擂台赛
        $arenacontestList = Arenacontest::with('organizerUser')
            ->limit(6)
            ->orderBy('game_datetime', 'desc')
            ->get();

        return $content
            ->header('看板')
            ->description('详情')
            ->row(view('admin.dashboard.title')
                ->with('totUserCount', $totUserCount)
                ->with('pkCount', $pkCount)
                ->with('missionsCount', $missionsCount)
                ->with('shareCount', $shareCount)
                ->with('item1', $item1)
                ->with('item2', $item2)
                ->with('arenacontestList', $arenacontestList)
                ->with('sectList', $sectList)
            );
    }
}
